//
//  XGVideoViewModel.swift
//  TodayNews
//
//  Created by monkey on 2020/4/1.
//  Copyright © 2020 itcast. All rights reserved.
//

import UIKit
import HandyJSON
import SwiftyJSON

class XGVideoViewModel
{
    /// 视频分类数组
    private(set) var videoCategories = [XGNewsCategoryModel]()
}

extension XGVideoViewModel
{
    /// 获取西瓜视频分类标题数据
    /// - Parameter completionHandle: 完成回调
    func fetchVideoCategories(completionHandle: @escaping(Bool) -> Void) -> Void
    {
        AlamofireTool.loadVideoCategoriesData { (responseObject, error) in
            if responseObject == nil || error != nil {
                XGPrint("加载西瓜视频分类标题数据失败！")
                completionHandle(false)
                return
            }
            
            let jsonObject = JSON(responseObject!)
            guard let data = jsonObject["data"].array else {
                completionHandle(false)
                return
            }
            
            self.videoCategories = data.compactMap { return XGNewsCategoryModel.deserialize(from: $0.dictionaryObject)}
            completionHandle(true)
        }
    }
    
    func fetchVideoInfo(videoId: String,completionHandle: @escaping(Bool,XGVideoInfoModel?) -> Void) -> Void
    {
        AlamofireTool.loadVideoInfoData(videoId: videoId) { (responseObject, error) in
            if responseObject == nil || error != nil {
                XGPrint("加载西瓜视频数据失败!")
                completionHandle(false, nil)
                return
            }
            
            let jsonObject = JSON(responseObject!)
            let videoInfo = XGVideoInfoModel.deserialize(from: jsonObject["data"].dictionaryObject)
            completionHandle(true, videoInfo)
        }
    }
}
